package com.ujs.edu.demo.controller;

import com.ujs.edu.demo.config.NeedLogin;
import com.ujs.edu.demo.dao.BcBooksMapper;
import com.ujs.edu.demo.dataobject.BcBooks;
import com.ujs.edu.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class StockController {

    @Autowired
    private BcBooksMapper bcBooksMapper;



    @RequestMapping(path = "/stock")
    public ModelAndView stock(){
        ModelAndView view=new ModelAndView();
        List<BcBooks> bcBooks=bcBooksMapper.selectAll();
        view.addObject("books",bcBooks);
        view.setViewName("stock");
        return view;
    }


    //搜索相应的信息
    @RequestMapping(path = "/searchstock")
    @ResponseBody
    public ModelAndView searchstock(@RequestParam("searchmethod") String searchmethod,@RequestParam("input") String input){

        Result result = new Result();

        ModelAndView view = new ModelAndView();
        BcBooks bcBooks1=new BcBooks();
        if (searchmethod.equals("报刊编号")){
            bcBooks1.setBookNum(Integer.parseInt(input));
        }else if(searchmethod.equals("报刊名称")){
            bcBooks1.setBookName(input);
        }else if(searchmethod.equals("出版社")){
            bcBooks1.setAuthor(input);
        }else if(searchmethod.equals("分类")){
            bcBooks1.setCartId(input);
        }else {
            bcBooks1.setMaktAble(Integer.parseInt(input));
        }
        List<BcBooks> bcBooks = bcBooksMapper.dynamicFind(bcBooks1);
        view.addObject("books", bcBooks);
        view.setViewName("put");
        result.setStatus(1);
        result.setMsg("成功");
        return view;
    }

    //上架
    @RequestMapping(path = "/maktable")
    @ResponseBody
    public Result maktable(@RequestParam("bookId") Integer bookId) {
        Result result = new Result();
        int bc = bcBooksMapper.updatemaktable(bookId);
        if (bc == 1) {
            result.setStatus(1);
            result.setMsg("上架架成功");
        } else {
            result.setStatus(0);
            result.setMsg("上架失败");
        }
        return result;
    }

}
